<?php
class Redis_model extends Base_Model
{
    private $REDIS;
    private $host;
    private $port;
    private $pwd;
    private $user;
    public function __construct()
    {
        parent::__construct();
        $this->config->load('redis',true);
        $this->host = $this->config->item('host','redis');
        $this->port = $this->config->item('port','redis');
        $this->pwd  = $this->config->item('pwd','redis');
        $this->user = $this->config->item('user','redis');
        $this->REDIS = new Redis();
        $this->connect();
    }

    private function connect()
    {
        //建立连接
        if ($this->REDIS->connect($this->host, $this->port) == false) {
            die($this->REDIS->getLastError());
        }
        //认证
        if ($this->REDIS->auth($this->pwd) == false) {
            die($this->REDIS->getLastError());
        }
    }
    /**
     *
     */
    public function getRedisInstance()
    {
        return $this->REDIS;
    }
    /**
     * 设置字符串类型
     * @return bool
     */
    public function set($key,$value)
    {
        return $this->REDIS->set($key,$value);
    }
    /**
     * 设置位值
     * @return 0,1 int 之前该位的值
     */
    public function setbit($key,$offset,$val)
    {
        return $this->REDIS->setBit($key,$offset,$val);
    }

    public function bitcount($key)
    {
        return $this->REDIS->bitCount($key);
    }

    public function getbit($key,$offset)
    {
        return $this->REDIS->getBit($key,$offset);
    }

    public function get($key)
    {
        return $this->REDIS->get($key);
    }

    public function deletekey($key)
    {
        return $this->REDIS->del($key);
    }

    public function hmset($key,$fieldValue)
    {
        return $this->REDIS->hMset($key,$fieldValue);
    }

    public function hget($key,$field)
    {
        return $this->REDIS->hGet($key,$field);
    }

    public function hmget($key,$fields)
    {
        return $this->REDIS->hMGet($key,$fields);
    }

    public function sismember($key,$member)
    {
        return $this->REDIS->sIsMember($key,$member);
    }
    public function smembers($key)
    {
        return $this->REDIS->sMembers($key);
    }
    public function sadd($key,$value)
    {
        return $this->REDIS->sAdd($key,$value);
    }

    public function zincrby($key,$step,$member)
    {
        return $this->REDIS->zIncrBy($key,$step,$member);
    }

    public function hincrby($key,$field,$value)
    {
        return $this->REDIS->hIncrBy($key,$field,$value);
    }

    public function incrby($key,$value)
    {
        return $this->REDIS->incrBy($key,$value);
    }

    public function zrange($key,$start,$end,$withscore = false)
    {
        return $this->REDIS->zRange($key,$start,$end,$withscore);
    }
    public function zrevrange($key,$start,$end,$withscore = false)
    {
        return $this->REDIS->zRevRange($key,$start,$end,$withscore);
    }
    public function zcard($key)
    {
        return $this->REDIS->zCard($key);
    }
    public function lrange($key,$start,$end)
    {
        return $this->REDIS->lRange($key,$start,$end);
    }
    public function lpop($key)
    {
        return $this->REDIS->lPop($key);
    }
    public function rpush($key,$val)
    {
        return $this->REDIS->rPush($key,$val);
    }
    public function blpop($key)
    {
        return $this->REDIS->blPop($key,0);
    }
    public function llen($key)
    {
        return $this->REDIS->lLen($key);
    }
    public function expire($key,$ttl)
    {
        return $this->REDIS->expire($key,$ttl);
    }
}